package com.imooc.module.reponse;

import lombok.Data;

import java.io.Serializable;

/**
 * @Author Tim
 * @Description:
 * @Version 1.0
 */
@Data
public class RestResult<T> implements Serializable {
    private boolean success;
    private String errMsg;
    private T data;
    /**
     * 毫秒数
     */
    private Long timestamp = System.currentTimeMillis();
    private String code;

    public RestResult() {

    }
    public static<T> RestResult<T> success() {
        RestResult<T> result = new RestResult<>();
        result.success = true;
        result.data = null;
        result.setCode("200");
        return result;
    }

    public static<T> RestResult<T> success(T object) {
        RestResult<T> result = new RestResult<>();
        result.success = true;
        result.data = object;
        result.setCode("200");
        return result;
    }

    public static<T> RestResult<T> success(T object, String msg) {
        RestResult<T> result = new RestResult<>();
        result.success = true;
        result.data = object;
        result.setCode("200");
        result.setErrMsg(msg);
        return result;
    }

    public static<T> RestResult<T> error(String code) {
        RestResult<T> result = new RestResult<>();
        result.setSuccess(false);
        result.setCode(code);
        result.setErrMsg("parm error");
        return result;
    }
    public static<T> RestResult<T> error(String code, String errMsg) {
        RestResult<T> result = new RestResult<>();
        result.setSuccess(false);
        result.setCode(code);
        result.setErrMsg(errMsg);
        return result;
    }
}
